package com.example.clxy.service.impl;

import com.example.clxy.dao.ProgramMapper;
import com.example.clxy.entity.Experiment;
import com.example.clxy.entity.Program;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.PageResult.ExperimentPageResult;
import com.example.clxy.result.business.PageResult.PlatformPageResult;
import com.example.clxy.result.business.PageResult.ProgramPageResult;
import com.example.clxy.result.business.PageResult.SycoursePageResult;
import com.example.clxy.service.ProgramService;
import com.example.clxy.util.RespUtil;
import com.example.clxy.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author weichong
 * date 2020-08-04
 * @Description:
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProgramServiceImpl implements ProgramService {

    @Autowired
    private ProgramMapper programMapper;


    @Override
    public WebRestResult addProgram(Program program){
        if(program.getProgramname() == null || program.getProgramname().isEmpty()){
            return RespUtil.failResp("培养方案名不能为空!");
        }
        program.setProgramid(UUIDUtil.randomUUID());
        program.setFlag("0");
        program.setStatus("0");
        program.setCreatedTime(new Date());
        if(programMapper.insert(program)==1){
            return RespUtil.successResp("成功新增培养方案!");
        }
        return RespUtil.failResp("新增培养方案失败!");
    }

    @Override
    public WebRestResult updateProgram(Program program){
        if(program.getProgramid() == null || program.getProgramid().isEmpty()){
            return RespUtil.failResp("培养方案id不能为空!");
        }
        program.setUpdatedTime(new Date());
        if(programMapper.updateByPrimaryKeySelective(program)==1){
            return RespUtil.successResp("成功更新培养方案!");
        }
        return RespUtil.failResp("更新培养方案失败!");
    }

    @Override
    public PageResult<ProgramPageResult> getProgramList(String programid, String programname, String year, String procode, String schoolname, String status, int page, int pagesize){
        PageResult<ProgramPageResult> res = new PageResult<>();
        int begin = (page-1) * pagesize;
        int end = pagesize;
        List<ProgramPageResult> programList = programMapper.getProgramList(programid,programname,year,procode,schoolname,status,begin,end,pagesize);
        if(programList.size()>0){
            res.setTotal(programList.get(0).getCnt());
            res.setPageCount(programList.get(0).getPage());
            res.setRows(programList);
            res.setResult(WebRestResult.SUCCESS);
            res.setMessage("查询培养方案信息成功");
        }
        return res;
    }

    @Override
    public WebRestResult addProgramSycourse(String programid,String sycourseid){
        if(programMapper.addProgramSycourse(programid,sycourseid) == 1){
            return RespUtil.successResp("新增培养方案与实验课程的关系成功");
        }
        return RespUtil.failResp("新增培养方案与实验课程的关系失败");
    }

    @Override
    public WebRestResult addProgramSycourseExperiment(String programid,String sycourseid,String experimentid){
        if(programMapper.addProgramSycourseExperiment(programid,sycourseid,experimentid) == 1){
            return RespUtil.successResp("新增培养方案下实验课程与子实验的关系成功");
        }
        return RespUtil.failResp("新增培养方案下实验课程与子实验的关系失败");
    }


    @Override
    public WebRestResult addProgramPlatform(String programid,String platformid){
        if(programMapper.addProgramPlatform(programid,platformid) == 1){
            return RespUtil.successResp("新增培养方案与平台的关系成功");
        }
        return RespUtil.failResp("新增培养方案与平台的关系失败");
    }

    @Override
    public WebRestResult addProgramPlatformExperiment(String programid,String platformid,String experimentid){
        if(programMapper.addProgramPlatformExperiment(programid,platformid,experimentid) == 1){
            return RespUtil.successResp("新增培养方案下平台与子实验的关系成功");
        }
        return RespUtil.failResp("新增培养方案下平台与子实验的关系失败");
    }

    @Override
    public WebRestResult deleteProgramSycourse(String programid,String sycourseid){
        if(programMapper.deleteProgramSycourse(programid,sycourseid) > 0){
            programMapper.deleteProgramSycourseExperiment(programid,sycourseid,null);
            return RespUtil.successResp("删除培养方案下的实验课程及该课程下的子实验成功");
        }
        return RespUtil.failResp("删除培养方案下的实验课程失败");
    }


    @Override
    public  WebRestResult deleteProgramSycourseExperiment(String programid,String sycourseid,String experimentid){
        if(programMapper.deleteProgramSycourseExperiment(programid,sycourseid,experimentid) > 0 ){
            return RespUtil.successResp("删除培养方案下实验课程下的子实验关系成功");
        }
        return RespUtil.failResp("删除培养方案下实验课程下的子实验关系失败");
    }

    @Override
    public WebRestResult deleteProgramPlatform(String programid,String platformid){
        if(programMapper.deleteProgramPlatform(programid,platformid) > 0){
            programMapper.deleteProgramPlatformExperiment(programid,platformid,null);
            return RespUtil.successResp("删除培养方案与平台及该平台与子实验的关系成功");
        }
        return RespUtil.failResp("删除培养方案与平台的关系失败");
    }

    @Override
    public WebRestResult deleteProgramPlatformExperiment(String programid,String platformid,String experimentid){
        if(programMapper.deleteProgramPlatformExperiment(programid,platformid,experimentid) > 0 ){
            return RespUtil.successResp("删除培养方案下平台与子实验的关系成功");
        }
        return RespUtil.failResp("删除培养方案下平台与子实验的关系失败");
    }




    @Override
    public PageResult<SycoursePageResult> getAllSycourseOfProgram(String programid, String sycoursename, int page, int pagesize){
        PageResult<SycoursePageResult> res = new PageResult<>();
        int begin = (page-1) * pagesize;
        int end = pagesize;
        List<SycoursePageResult> sycourseList = programMapper.getAllSycourseOfProgram(programid,sycoursename,begin,end,pagesize);
        if(sycourseList.size()>0){
            res.setTotal(sycourseList.get(0).getCnt());
            res.setPageCount(sycourseList.get(0).getPage());
            res.setRows(sycourseList);
            res.setResult(WebRestResult.SUCCESS);
            res.setMessage("查询培养方案下的所有实验课程信息成功");
        }
        return res;
    }

    @Override
    public PageResult<ExperimentPageResult> getAllExperimentOfProgramSycourse(String programid, String sycourseid, String experimentname, int page, int pagesize){
        PageResult<ExperimentPageResult> res = new PageResult<>();
        int begin = (page-1) * pagesize;
        int end = pagesize;
        List<ExperimentPageResult> experimentList = programMapper.getAllExperimentOfProgramSycourse(programid,sycourseid,experimentname,begin,end,pagesize);
        if(experimentList.size()>0){
            res.setTotal(experimentList.get(0).getCnt());
            res.setPageCount(experimentList.get(0).getPage());
            res.setRows(experimentList);
            res.setResult(WebRestResult.SUCCESS);
            res.setMessage("查询培养方案下该实验课程下的所有子实验信息成功");
        }
        return res;
    }

    @Override
    public PageResult<SycoursePageResult> getProgramSycourseExperiment(String programid, int page, int pagesize){
        PageResult<SycoursePageResult> res = new PageResult<>();
        int begin = (page-1) * pagesize;
        int end = pagesize;
        List<SycoursePageResult> sycourseList = programMapper.getAllSycourseOfProgram(programid,null,begin,end,pagesize);
        if(sycourseList.size()>0){
            for(SycoursePageResult sycourse : sycourseList){
                List<Experiment> experimentList = programMapper.getExperimentListByProgramSycourse(programid,sycourse.getSycourseid());
                System.out.println(experimentList.get(0).getExperimentname());
                sycourse.setExperimentList(experimentList);
            }
            res.setTotal(sycourseList.get(0).getCnt());
            res.setPageCount(sycourseList.get(0).getPage());
            res.setRows(sycourseList);
        }
        res.setResult(WebRestResult.SUCCESS);
        res.setMessage("查询该培养方案下所有实验课程及子实验信息成功");
        return res;

    }



    @Override
    public PageResult<PlatformPageResult> getAllPlatformOfProgram(String programid, String platformname, int page, int pagesize){
        PageResult<PlatformPageResult> res = new PageResult<>();
        int begin = (page-1) * pagesize;
        int end = pagesize;
        List<PlatformPageResult> platformList = programMapper.getAllPlatformOfProgram(programid,platformname,begin,end,pagesize);
        if(platformList.size()>0){
            res.setTotal(platformList.get(0).getCnt());
            res.setPageCount(platformList.get(0).getPage());
            res.setRows(platformList);
            res.setResult(WebRestResult.SUCCESS);
            res.setMessage("查询培养方案下的所有平台信息成功");
        }
        return res;
    }

    @Override
    public PageResult<ExperimentPageResult> getAllExperimentOfProgramPlatform(String programid, String platformid, String experimentname, int page, int pagesize){
        PageResult<ExperimentPageResult> res = new PageResult<>();
        int begin = (page-1) * pagesize;
        int end = pagesize;
        List<ExperimentPageResult> experimentList = programMapper.getAllExperimentOfProgramPlatform(programid,platformid,experimentname,begin,end,pagesize);
        if(experimentList.size()>0){
            res.setTotal(experimentList.get(0).getCnt());
            res.setPageCount(experimentList.get(0).getPage());
            res.setRows(experimentList);
            res.setResult(WebRestResult.SUCCESS);
            res.setMessage("查询培养方案下该平台下的所有子实验信息成功");
        }
        return res;
    }

    @Override
    public PageResult<PlatformPageResult> getProgramPlatformExperiment(String programid, int page, int pagesize){
        PageResult<PlatformPageResult> res = new PageResult<>();
        int begin = (page-1) * pagesize;
        int end = pagesize;
        List<PlatformPageResult> platformList = programMapper.getAllPlatformOfProgram(programid,null,begin,end,pagesize);
        if(platformList.size()>0){
            for(PlatformPageResult platform : platformList){
                List<Experiment> experimentList = programMapper.getExperimentListByProgramPlatform(programid,platform.getPlatformid());
                platform.setExperimentList(experimentList);
            }
            res.setTotal(platformList.get(0).getCnt());
            res.setPageCount(platformList.get(0).getPage());
            res.setRows(platformList);
        }
        res.setResult(WebRestResult.SUCCESS);
        res.setMessage("查询该培养方案下所有平台信息及子实验信息成功");
        return res;
    }
    @Override
    public WebRestResult copyProgram(String oldprogramid, String newprogramid){
        Program oldprogram = programMapper.selectByPrimaryKey(oldprogramid);
        Program newprogram = programMapper.selectByPrimaryKey(newprogramid);
        if(oldprogram == null || oldprogram.getFlag() == "1"){
            return RespUtil.failResp("旧培养方案不存在");
        }
        if(newprogram == null || newprogram.getFlag() == "1"){
            return RespUtil.failResp("新培养方案不存在");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("oldprogramid",oldprogramid);
        map.put("newprogramid",newprogramid);
        programMapper.copyProgram(map);
        if(map.get("isrollback").toString().equals("1")){
            return RespUtil.failResp("复制失败");
        }
        return RespUtil.successResp("复制成功");
    }

}
